home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / rootkits / rootkit / mbuf.c < prev    next >
Text File  |  1994-03-01  |  8KB  |  268 lines

  1. /*
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  */
  14.  
  15. #ifndef lint
  16. static    char sccsid[] = "@(#)mbuf.c 1.1 91/11/13 SMI"; /* from UCB 5.6 6/29/88 */
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <malloc.h>
  21. #include <sys/param.h>
  22. #include <sys/mbuf.h>
  23. #include <sys/stream.h>
  24. #include <sys/strstat.h>
  25. #define    YES    1
  26. typedef int bool;
  27.  
  28. struct    mbstat mbstat;
  29. extern    int kread();
  30.  
  31. static struct mbtypes {
  32.     int    mt_type;
  33.     char    *mt_name;
  34. } mbtypes[] = {
  35.     { MT_DATA,    "data" },
  36.     { MT_HEADER,    "packet headers" },
  37.     { MT_SOCKET,    "socket structures" },
  38.     { MT_PCB,    "protocol control blocks" },
  39.     { MT_RTABLE,    "routing table entries" },
  40.     { MT_HTABLE,    "IMP host table entries" },
  41.     { MT_ATABLE,    "address resolution tables" },
  42.     { MT_FTABLE,    "fragment reassembly queue headers" },
  43.     { MT_SONAME,    "socket names and addresses" },
  44.     { MT_ZOMBIE,    "zombie process information" },
  45.     { MT_SOOPTS,    "socket options" },
  46.     { MT_RIGHTS,    "access rights" },
  47.     { MT_IFADDR,    "interface addresses" }, 
  48.     { 0, 0 }
  49. };
  50.  
  51. int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
  52. bool seen[256];            /* "have we seen this type yet?" */
  53.  
  54. /*
  55.  * Print mbuf statistics.
  56.  */
  57. mbpr(mbaddr)
  58.     off_t mbaddr;
  59. {
  60.     register int totmem, totfree, totmbufs;
  61.     register int i;
  62.     register struct mbtypes *mp;
  63.  
  64.     if (nmbtypes != 256) {
  65.         fprintf(stderr, "unexpected change to mbstat; check source\n");
  66.         return;
  67.     }
  68.     if (mbaddr == 0) {
  69.         printf("mbstat: symbol not in namelist\n");
  70.         return;
  71.     }
  72.     kread(mbaddr, &mbstat, sizeof (mbstat));
  73.     printf("%d/%d mbufs in use:\n",
  74.         mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE], mbstat.m_mbufs);
  75.     totmbufs = 0;
  76.     /*
  77.      * Tally up totals for known mbuf types.
  78.      */
  79.     for (mp = mbtypes; mp->mt_name; mp++)
  80.         if (mbstat.m_mtypes[mp->mt_type]) {
  81.             seen[mp->mt_type] = YES;
  82.             printf("\t%d mbufs allocated to %s\n",
  83.                 mbstat.m_mtypes[mp->mt_type], mp->mt_name);
  84.             totmbufs += mbstat.m_mtypes[mp->mt_type];
  85.         }
  86.     seen[MT_FREE] = YES;
  87.     /*
  88.      * Tally up totals for unknown mbuf types
  89.      * (ones that don't appear in the mbtypes table).
  90.      */
  91.     for (i = 0; i < nmbtypes; i++)
  92.         if (!seen[i] && mbstat.m_mtypes[i]) {
  93.             printf("\t%d mbufs allocated to <mbuf type %d>\n",
  94.                 mbstat.m_mtypes[i], i);
  95.             totmbufs += mbstat.m_mtypes[i];
  96.         }
  97.     if (totmbufs != mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE])
  98.         printf("*** %d mbufs missing ***\n",
  99.             (mbstat.m_mbufs - mbstat.m_mtypes[MT_FREE]) - totmbufs);
  100.     printf("%d/%d cluster buffers in use\n",
  101.         mbstat.m_clusters - mbstat.m_clfree, mbstat.m_clusters);
  102.     /*
  103.      * XXX:    Should account for MCL_LOANED mbufs here, too, but that
  104.      *    requires examining all the mbuf headers.
  105.      */
  106.     totmem = mbstat.m_mbufs * MSIZE + mbstat.m_clusters * MCLBYTES;
  107.     totfree = mbstat.m_mtypes[MT_FREE]*MSIZE + mbstat.m_clfree * MCLBYTES;
  108.     printf("%d Kbytes allocated to network (%d%% in use)\n",
  109.         totmem / 1024, (totmem - totfree) * 100 / totmem);
  110.     printf("%d requests for memory denied\n", mbstat.m_drops);
  111.     printf("%u requests for memory delayed\n", mbstat.m_wait);
  112.     printf("%u calls to protocol drain routines\n", mbstat.m_drain);
  113. }
  114.  
  115.  
  116. /*
  117.  * Streams allocation statistics code.
  118.  */
  119.  
  120. struct strstat    strst;
  121. u_int        nstrbufsz;
  122. alcdat        *strbufstat;
  123.  
  124. /*
  125.  * Print statistics for streams allocation.
  126.  */
  127. strstpr(straddr, nstrbufszaddr, strbufsizesaddr, strbufstataddr)
  128.     off_t    straddr;
  129.     off_t    nstrbufszaddr;
  130.     off_t    strbufsizesaddr;
  131.     off_t    strbufstataddr;
  132. {
  133.     register int        i;
  134.     register u_int        size;
  135.     register struct strstat    *s = &strst;
  136.     register u_int        *strbufsizes;
  137.     register alcdat        *sbs;
  138.     char            buf[50];
  139.  
  140.     /*
  141.      * Maximize the probability of getting an internally consistent
  142.      * snapshot by gathering all data before printing anything.
  143.      */
  144.  
  145.     if (straddr == 0 || nstrbufszaddr == 0 || strbufsizesaddr == 0 ||
  146.         strbufstataddr == 0)
  147.         return;
  148.  
  149.     /*
  150.      * Grab the strst structure (which contains overall STREAMS
  151.      * statistics).
  152.      */
  153.     if (kread(straddr, (char *)&strst, sizeof strst) != sizeof strst) {
  154.         printf("prstrst: bad read\n");
  155.         return;
  156.     }
  157.  
  158.     /*
  159.      * Get number of buckets for actual buffer allocations.
  160.      */
  161.     if (kread(nstrbufszaddr, (char *)&nstrbufsz, sizeof nstrbufsz) !=
  162.         sizeof nstrbufsz) {
  163.         (void) fprintf(stderr, "strstpr: bad read of nstrbufsz\n");
  164.         return;
  165.     }
  166.  
  167.     /*
  168.      * Allocate space for the strbufsizes array and pull it in.
  169.      */
  170.     size = sizeof *strbufsizes * nstrbufsz;
  171.     strbufsizes = (u_int *) malloc(size);
  172.     if (strbufsizes == NULL) {
  173.         (void) fprintf(stderr, "strstpr: bad malloc of strbufsizes\n");
  174.         return;
  175.     }
  176.     if (kread(strbufsizesaddr, (char *)strbufsizes, size) != size) {
  177.         (void) fprintf(stderr, "strstpr: bad read of strbufsizes\n");
  178.         return;
  179.     }
  180.  
  181.     /*
  182.      * Allocate space for the strbufstat array and pull it in.  Note that
  183.      * it contains two extra entries at the beginning for external buffers
  184.      * and buffers embedded in dblks.  Also, since the kernel itself
  185.      * dynamically allocates space for this array, we have to go through
  186.      * an extra level of indirection here.
  187.      */
  188.     if (kread(strbufstataddr, (char *)&strbufstat, sizeof strbufstat) !=
  189.         sizeof strbufstat) {
  190.         (void) fprintf(stderr, "strstpr: bad read of strbufstat\n");
  191.         return;
  192.     }
  193.     size = sizeof *sbs * (2 + nstrbufsz);
  194.     sbs = (alcdat *) malloc(size);
  195.     if (strbufstat == NULL) {
  196.         (void) fprintf(stderr,
  197.                 "strstpr: bad malloc of strbufstat array\n");
  198.         return;
  199.     }
  200.     if (kread((off_t)strbufstat, (char *)sbs, size) != size) {
  201.         (void) fprintf(stderr,
  202.                 "strstpr: bad read of strbufstat array\n");
  203.         return;
  204.     }
  205.  
  206.     /*
  207.      * Display overall STREAMS data.
  208.      */
  209.     printf("streams allocation:\n");
  210.     printf("%*s%s\n", 41, "", "cumulative  allocation");
  211.     printf("%*s%s\n", 22, "", "current   maximum       total    failures");
  212.     pf_strstat("streams", &s->stream);
  213.     pf_strstat("queues", &s->queue);
  214.     pf_strstat("mblks", &s->mblock);
  215.     pf_strstat("dblks", &s->dblock);
  216.  
  217.     /*
  218.      * Display buffer-related data.
  219.      */
  220.     printf("streams buffers:\n");
  221.     pf_strstat("external", &sbs[0]);
  222.     pf_strstat("within-dblk", &sbs[1]);
  223.     for (i = 0; i < nstrbufsz - 1; i++) {
  224.         (void) sprintf(buf, "size <= %5d", strbufsizes[i]);
  225.         pf_strstat(buf, &sbs[i + 2]);
  226.     }
  227.     (void) sprintf(buf, "size >  %5d", strbufsizes[nstrbufsz - 2]);
  228.     pf_strstat(buf, &sbs[nstrbufsz + 1]);
  229. }
  230.  
  231. /*
  232.  * Print a line of streams allocation information, as recorded
  233.  * in the (alcdat *) given as argument.
  234.  */
  235. pf_strstat(label, alp)
  236.     char    *label;
  237.     alcdat    *alp;
  238. {
  239.     printf("%s%*s%6d    %6d     %7d      %6d\n",
  240.         label,
  241.         23 - strlen(label), "",    /* Move to column 24 */
  242.         alp->use,
  243.         alp->max,
  244.         alp->total,
  245.         alp->fail);
  246. }
  247.  
  248. #ifdef    notdef
  249. /*
  250.  * Streams display layout follows.
  251.  */
  252. streams allocation:
  253.                                          cumulative  allocation
  254.                       current   maximum       total    failures
  255. streams                000000    000000     0000000      000000
  256. queues                 000000    000000     0000000      000000
  257. mblks                  000000    000000     0000000      000000
  258. dblks                  000000    000000     0000000      000000
  259. streams buffers:
  260. external               000000    000000     0000000      000000
  261. within-dblk            000000    000000     0000000      000000
  262. size <=   ll           000000    000000     0000000      000000
  263. size <=  mmm           000000    000000     0000000      000000
  264. size <= nnnn           000000    000000     0000000      000000
  265. size >  nnnn           000000    000000     0000000      000000
  266. #endif    notdef
  267.  
  268.